# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="hclee"
__date__ ="$Dec 22, 2009 11:57:21 AM$"

from ami_util import AMIQuery

if __name__ == "__main__":
#    an example of using the AMIQuery class to search for ATLAS datasets and files
#    'entity' and 'attributes' to select can be found at http://ami.in2p3.fr/opencms/opencms/AMI/www/

    amiQry = AMIQuery()
    amiQry.entity = 'dataset'
    amiQry.select = [ 'logicalDatasetName', 'identifier' ]
    amiQry.query  = 'dataType=AOD projectName=data09_900GeV runNumber>=140541 version=f186 streamName=physics_MuonswBeam'
    amiQry.debug  = True

    ds_results = amiQry.runQuery()

    for ds_item in ds_results:
        ds = ds_item[ 'logicalDatasetName'.lower() ]
        id = ds_item[ 'identifier'.lower() ]

        print id, ds

        amiQry.entity = 'files'
        amiQry.select = [ 'LFN', 'lumiBN', 'events', 'fileGUID' ]
        amiQry.query  = 'LFN=%s datasetFK=%d' % ( ds, int(id) )

        f_results = amiQry.runQuery()

        for f_item in f_results:
            lfn    = f_item[ 'LFN'.lower() ]
            lumibn = f_item[ 'lumiBN'.lower() ]
            events = f_item[ 'events'.lower() ]
            guid   = f_item[ 'fileGUID'.lower() ]

            print '|- %s %d %d %s' % (guid, int(lumibn), int(events), lfn)
    
